<?php

namespace App\Modules\LuckDraw\Award;

use App\Http\Model\Mall;
use Illuminate\Support\Facades\Mail;

class GoodsAward extends  BaseAward implements AwardInterface {
    /**
     * @var mixed
     */
    public $type = 2;//类型 0未中奖 1:礼物 2:商品 3:金豆礼物/珍珠礼物

    public function __construct(
        int $luckId,
        int $userId,
        int $awardId,
        int $timeLimit = 0,
        int $price
    ){
        $this->luckId = $luckId;
        $this->userId = $userId;
        $this->awardId = $awardId;
        $this->timeLimit = $timeLimit;
        $this->price = $price;
    }
    public function save() : bool{
        // TODO: Implement save() method.

        // 增加到用户商城
        $mallModel = new Mall();
        // 查询商品
        $goodsData = $mallModel->get_goods($this->awardId);
        if (empty($goodsData)) {
            return false;
        }

        // 查询商品规格
        $goodsSkuData = $mallModel->getSkuDataByGoods($this->awardId);
        if (empty($goodsSkuData)) {
            return false;
        }

        $goodsSku = [];
        foreach ($goodsSkuData as $value) {
            if ($value['time_limit'] == $this->timeLimit) {
                $goodsSku = $value;
                break;
            }
        }

        if (empty($goodsSku)){
            return false;
        }

        $price = $goodsSku['price'] ?? 0;// 价格

        // 增加到用户商城
        $shoppingAct = $mallModel->shoppingAct($this->userId, $this->userId, $this->awardId, $goodsData['goods_name'], $goodsData['goods_type'], $goodsSku['sku_id'] ?? 0, $this->timeLimit, $price, $this->timeLimit * 24 * 3600 * $this->num, time(), 3);
        if (empty($shoppingAct) or empty($shoppingAct['id'] ?? '')) {
            return false;
        }
        return true;
    }
    public function detail() {
        $mallModel = new Mall();
        $goodsData = $mallModel->get_goods($this->awardId);
//        $goodsSkuData = $mallModel->getSkuDataByGoods($this->awardId);
//        $goodsSku = [];
//        foreach ($goodsSkuData as $value) {
//            if ($value['time_limit'] == $this->timeLimit) {
//                $goodsSku = $value;
//                break;
//            }
//        }

        $this->name = $goodsData['goods_name'];
        $this->image = $goodsData['small_img'];
        $this->animateResource = $goodsData['animate_resource']??'';
//        $this->price = $goodsSku['price'] ?? 0;
        $this->_getAperture();
    }

    private function _getAperture(){
        if ($this->price >= 0 and $this->price < 100000) {
            $this->aperture = 1;
        } elseif ($this->price >= 100000 and $this->price < 300000) {
            $this->aperture = 2;
        } elseif ($this->price >= 300000) {
            $this->aperture = 3;
        } else {
            $this->aperture = 0;
        }
    }
}